package com.jctrip.hotel.source.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.domain.dao.HtOrderSourceRecordDao;
import com.jctrip.hotel.domain.entity.HtOrderSourceRecord;
import com.jctrip.hotel.source.model.OrderDataResult;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.dossen.*;
import com.jctrip.hotel.source.model.hmein.req.QueryOrderStatusReq;
import com.jctrip.hotel.source.model.hmein.resp.QueryOrderStatusResult;
import com.jctrip.hotel.source.service.dossen.DossenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Api(tags = "dossen")
@RestController
@RequestMapping("/dossen")
@ApiSupport(order = 3)
public class DossenController {
    @Resource
    private DossenService dossenService;

    @GetMapping("/getCitiesList")
    @ApiOperation(value = "getCitiesList", notes = "获取城市列表")
    @ApiOperationSupport(order = 1)
    public Result getCitiesList(@RequestParam(value = "appId", required = false) String appId,
                                @RequestParam(value = "key", required = false) String key) {
        List<DossenCityData> result;
        try {
            result = dossenService.getCitiesList(appId, key);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getHotelInfo")
    @ApiOperation(value = "getHotelInfo", notes = "获取酒店详情")
    @ApiOperationSupport(order = 2)
    public Result getHotelInfo(@RequestParam(value = "appId", required = false) String appId,
                               @RequestParam(value = "key", required = false) String key,
                               @RequestBody DossenGetHotelInfoReq dossenGetHotelInfoReq) {
        Page<DossenHotelData> result;
        try {
            result = dossenService.getHotelInfo(appId, key, dossenGetHotelInfoReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getHotelRoomType")
    @ApiOperation(value = "getHotelRoomType", notes = "获取酒店房型")
    @ApiOperationSupport(order = 3)
    public Result getHotelRoomType(@RequestParam(value = "appId", required = false) String appId,
                                   @RequestParam(value = "key", required = false) String key,
                                   @RequestBody DossenGetHotelRoomTypeReq dossenGetHotelRoomTypeReq) {
        List<DossenRoomTypeData> result;
        try {
            result = dossenService.getHotelRoomType(appId, key, dossenGetHotelRoomTypeReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getRatePlan")
    @ApiOperation(value = "getRatePlan", notes = "获取价格规则")
    @ApiOperationSupport(order = 4)
    public Result getRatePlan(@RequestParam(value = "appId", required = false) String appId,
                              @RequestParam(value = "key", required = false) String key) {
        List<DossenRatePlanData> result;
        try {
            result = dossenService.getECRateplan(appId, key);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getPriceAndCount")
    @ApiOperation(value = "getPriceAndCount", notes = "获取房价信息")
    @ApiOperationSupport(order = 5)
    public Result getPriceAndCount(@RequestParam(value = "appId", required = false) String appId,
                                   @RequestParam(value = "key", required = false) String key,
                                   @RequestBody DossenGetPriceAndCountReq dossenGetPriceAndCountReq) {
        List<DossenPriceAndCountData> result;
        try {
            result = dossenService.getPriceAndCount(appId, key, dossenGetPriceAndCountReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/checkPriceAndCount")
    @ApiOperation(value = "checkPriceAndCount", notes = "校验实时房价房量")
    @ApiOperationSupport(order = 6)
    public Result checkPriceAndCount(@RequestParam String appId,
                                     @RequestParam String key,
                                     @RequestBody DossenCheckPriceAndCountReq dossenCheckPriceAndCountReq) {
        List<DossenPriceAndCountData> result;
        try {
            result = dossenService.checkPriceAndCount(appId, key, dossenCheckPriceAndCountReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    /**
     * 直连下单
     *
     * @param appId
     * @param key
     * @param dossenAddOrderReq
     * @return
     */
    @PostMapping("/addOrder")
    @ApiOperation(value = "addOrder", notes = "直连下单")
    @ApiOperationSupport(order = 7)
    public Result addOrder(@RequestParam String appId,
                           @RequestParam String key,
                           @RequestBody DossenAddOrderReq dossenAddOrderReq) {
        OrderDataResult result;
        try {
            result = dossenService.postOrder(appId, key, dossenAddOrderReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }


    @PostMapping("/getOrder")
    @ApiOperation(value = "getOrder", notes = "获取订单信息")
    @ApiOperationSupport(order = 8)
    public Result getOrder(@RequestParam(value = "appId", required = false) String appId,
                           @RequestParam(value = "key", required = false) String key,
                           @RequestBody DossenGetOrdersReq dossenGetOrdersReq) {
        DossenOrderData result;
        try {
            result = dossenService.getOrder(appId, key, dossenGetOrdersReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/cancelOrder")
    @ApiOperation(value = "cancelOrder", notes = "取消订单")
    @ApiOperationSupport(order = 9)
    public Result cancelOrder(@RequestParam(value = "appId", required = false) String appId,
                              @RequestParam(value = "key", required = false) String key,
                              @RequestBody DossenCancelOrderReq dossenCancelOrderReq) {
        try {
            dossenService.cancelOrder(appId, key, dossenCancelOrderReq);
            ;
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }


    //================接口直连================
    @GetMapping("/getDirectOrder")
    @ApiOperation(value = "getDirectOrder", notes = "接口直连查询订单信息")
    public Result getDirectOrder(@RequestParam(value = "sourceId") @ApiParam("供应商账户id") String sourceId,
                                 @RequestParam(value = "outOrderId") @ApiParam("渠道订单号") String outOrderId) {
        DossenOrderData order;
        try {
            DossenGetOrdersReq dossenGetOrdersReq = DossenGetOrdersReq.builder()
                    .outOrderId(outOrderId.trim())
                    .pageIndex(1)
                    .pageSize(20)
                    .build();
            HtOrderSourceRecordDao htOrderSourceRecordDao =
                    BeanContext.getApplicationContext().getBean(HtOrderSourceRecordDao.class);
            HtOrderSourceRecord sourceRecord = Optional.ofNullable(htOrderSourceRecordDao.selectOne(new LambdaQueryWrapper<HtOrderSourceRecord>()
                    .eq(HtOrderSourceRecord::getBookId, outOrderId.trim()))).orElse(new HtOrderSourceRecord());
            dossenGetOrdersReq.setResvId(sourceRecord.getSourceOrderId());
            order = dossenService.getDirectOrder(sourceId, dossenGetOrdersReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(order);
    }

}
